﻿using Sttplay.Net.UDP;
using Sttplay.Transmission.TCP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TCPServerConcurrentTest
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 1; i++)
            {
                new Thread(UDPTest).Start();
            }
            new Thread(FPS).Start();
            Console.Read();
        }
        public static uint GetTimestamp()
        {
            //获取从1970年一月一日0点0分0秒0微妙开始
            ulong ms = (ulong)(DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks) / 10000;
            return (uint)(ms & 0xfffffffful);
        }
        static int connectCount = 0;
        static void FPS()
        {
            
            while(true)
            {
                Console.WriteLine(connectCount);
                Thread.Sleep(1000);
            }
        }
        static void TCPTest()
        {
            TCPClient client = new TCPClient();
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            bool isExit = false;
            client.onConnectFailedEvent += (s) =>
            {
                isExit = true;
            };
            client.onConnectSuccessedEvent += () =>
            {
                Interlocked.Increment(ref connectCount);
                byte[] buffer = Encoding.UTF8.GetBytes("hellossssssssssssssssssssssss" +
                    "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
                    "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
                    "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
                    "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
                    "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
                    "ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
                    "ssssss");
                client.Send(buffer, 0, buffer.Length);
                client.Disconnect();
                isExit = true;
            };
            client.onRecvEvent += (s) =>
            {

            };
            for (int i = 0; i < 10000; i++)
            {
                isExit = false;
                client.AsyncConnect(ip, 5555);
                
                while (!isExit)
                {
                    client.Update();
                    Thread.Sleep(10);
                }
                client.Disconnect();
                GC.Collect();
            }
          
        }
        static void UDPTest()
        {
            RUDP rudp = new RUDP();
            rudp.Initialize(IPAddress.Any, 0);
            IPEndPoint remote = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 5556);
            byte[] buffer = new byte[1350];
            while(true)
            {
                rudp.SendTo(buffer, 0, buffer.Length, remote, false);
            }
        }
    }
}
